In-class Exercise 5

A short description of the post.

Lye Jia Wei https://lye-jia-wei.github.io/
09-13-2021

Important to set fig.retina to be 3 in order for subsequent visualization to be sharp.

Installing and Loading the R package

packages <- c('maptools','sf','raster','spatstat','tmap','tidyverse')
for (p in packages){
  if(!require(p, character.only = T)){
    install.packages(p)
  }
}
library(p, character.only=T)

Import the Geospatial Data

The code chunk import shapefile using st_read() of sf package. The output object is in tibble sf object class.

Pgl_sf and mpsz_sf geospatial data sets into RStudio.

Note: - Do not paste directory in dsn

Pgl_sf <- st_read(dsn="data/shapefile", layer="Punggol_St")
Reading layer `Punggol_St' from data source 
  `C:\Users\User\Desktop\IS415\lye-jia-wei\IS415_blog\_posts\2021-09-13-in-class-exercise-5\data\shapefile' 
  using driver `ESRI Shapefile'
Simple feature collection with 2642 features and 2 fields
Geometry type: LINESTRING
Dimension:     XY
Bounding box:  xmin: 34038.56 ymin: 40941.11 xmax: 38882.85 ymax: 44801.27
Projected CRS: SVY21 / Singapore TM
mpsz_sf <- st_read(dsn="data/shapefile",layer="MP14_SUBZONE_WEB_PL")
Reading layer `MP14_SUBZONE_WEB_PL' from data source 
  `C:\Users\User\Desktop\IS415\lye-jia-wei\IS415_blog\_posts\2021-09-13-in-class-exercise-5\data\shapefile' 
  using driver `ESRI Shapefile'
Simple feature collection with 323 features and 15 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: 2667.538 ymin: 15748.72 xmax: 56396.44 ymax: 50256.33
Projected CRS: SVY21

Projection is SVY21

Import aspatial data from rds folder

read_rds() of readr package is used instead of readRDS() of base R is used. This is because output of read_rds() is in tibble object.

Note:

childcare <- read_rds("data/rds/childcare.rds")
CHAS <- read_rds("data/rds/CHAS.rds")

Converting the aspatial data frame into sf objects

CHAS_sf <- st_as_sf(CHAS, coords = c("X_COORDINATE","Y_COORDINATE"),crs=3414)


childcare_sf <- st_as_sf(childcare, coords= c("Lng","Lat"),crs=4326) %>% st_transform(crs=3414)

Note:

Plotting for Reviewing

Alpha is to set transparency

tmap_mode("view")
tm_shape(childcare_sf)+tm_dots(alpha= 0.4, col="blue", size=0.05)+ tm_shape(CHAS_sf)+tm_dots(alpha= 0.4, col="red", size=0.05)

Note:

Geospatial Data Wrangling

Converting from sf to Spatial* data frame

Polygon will be converted so spatial polygon object, point will be converted to spatial point.

childcare <- as_Spatial(childcare_sf)
CHAS <- as_Spatial(CHAS_sf)
mpsz <- as_Spatial(mpsz_sf)

Converting Spatial* data frame into Spatial* Objects